# -*- coding:utf-8 -*-
# 爬虫
# 目标网站：https://cs.anjuke.com/sale/yuelu/
# 任务需求：
# 1.使用xpath解析
# 2.抓取第一页：标题，户型，面积，楼层，建造时间，位置，总价，单价
# 3.要求存储在MySQL和MongoDB

import requests
import pymysql
import pymongo
from lxml import etree


class ResoldApartment(object):
    def __init__(self):
        self.url = "https://cs.anjuke.com/sale/yuelu/"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
        }

    # 先目标网站发起请求
    def get_data_index(self):
        response = requests.get(self.url, headers=self.headers)
        response.encoding = "utf-8"
        if response.status_code == 200:
            return response.text
        else:
            return None

    # 数据解析
    def parse_data(self, response):
        html = etree.HTML(response)
        house_list = html.xpath('//section[@class="list-left"]/section[2]//div[@class="property"]')
        for data in house_list:
            title = data.xpath("./a/div[2]/div[1]/div[1]/h3/text()")[0].strip() + "\n"
            house_one = "".join(data.xpath("./a/div[2]/div[1]/section/div[1]/p[1]//span/text()"))
            house_two = data.xpath("./a/div[2]/div[1]/section/div[1]/p[2]/text()")[0].strip()
            house_three = data.xpath("./a/div[2]/div[1]/section/div[1]/p[3]/text()")[0]
            try:
                house_four = data.xpath("./a/div[2]/div[1]/section/div[1]/p[4]/text()")[0].strip()
            except IndexError:
                house_four = " "
            try:
                house_five = data.xpath("./a/div[2]/div[1]/section/div[1]/p[5]/text()")[0].strip() + "\n"
            except IndexError:
                house_five = " "
            house_info = "".join(data.xpath("./a/div[2]/div[1]/section/div[2]/p[1]/text()"))
            house_info_two = "-".join(data.xpath("./a/div[2]/div[1]/section/div[2]/p[2]//span/text()"))
            price = "".join(data.xpath("./a/div[2]/div[2]/p[1]//span/text()")) + "\n"
            try:
                price_one = data.xpath("./a/div[2]/div[2]/p[2]//span/text")[0]
            except IndexError:
                price_one = " "
            print(title, house_one, house_two, house_three, house_four, house_five, house_info, house_info_two, price, price_one)

    # 业务实现
    def main(self):
        response = self.get_data_index()
        # print(response)
        self.parse_data(response)


if __name__ == '__main__':
    data = ResoldApartment()
    data.main()
